package com.zkingsoft.services.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.CaseStepDao;
import com.zkingsoft.dao.UserCaseDao;
import com.zkingsoft.model.CaseStep;
import com.zkingsoft.model.UserCase;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.services.UserCaseService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-02-05 10:32
 */
@Service("userCaseService")
public class UserCaseServiceImpl implements UserCaseService {

	@Autowired
	private UserCaseDao userCaseDao;
	@Autowired
	private CaseStepDao stepDao;

	@Transactional
	@Override
	public int add(UserCase userCase) {
		int i = userCaseDao.insert(userCase);
		setSteps(userCase);
		return i;

	}

	@Override
	public int batchAdd(List<UserCase> userCaseList) {
		return userCaseDao.batchInsert(userCaseList);
	}

	@Transactional
	@Override
	public int modify(UserCase userCase) {
		setSteps(userCase);
		return userCaseDao.updateByModel(userCase);
	}

	private void setSteps(UserCase userCase) {
		// 删除historyNo为0的步骤
		CaseStep del = new CaseStep();
		del.setHistoryNo(0);
		del.setCateId(userCase.getCaseId());
		stepDao.deleteByModel(del);
		List<CaseStep> steps = new ArrayList<>();
		for (CaseStep caseStep : userCase.getStepsNoHistory()) {
			if (caseStep.getStepNo() != null) {
				caseStep.setCateId(userCase.getCaseId());
				caseStep.setHistoryNo(0);
				steps.add(caseStep);
			}
		}
		// 插入步骤
		if (!steps.isEmpty())
			stepDao.batchInsert(steps);
	}

	@Override
	public int remove(List<Long> list) {
		for (Long long1 : list) {
			CaseStep del = new CaseStep();
			if(long1!=null){
				del.setCateId(long1);
				stepDao.deleteByModel(del);
			}
		}
		if(!list.isEmpty()){
			return userCaseDao.deleteByIds(list);
		}else{
			return 1;
		}
		
	}

	@Override
	public int removeById(Long caseId) {
		// 删除historyNo为0的步骤
		CaseStep del = new CaseStep();
		del.setCateId(caseId);
		stepDao.deleteByModel(del);
		return userCaseDao.deleteById(caseId);

	}

	@Override
	public int removeByModel(UserCase userCase) {

		return userCaseDao.deleteByModel(userCase);

	}

	@Override
	public List<UserCase> findInPage(UserCase userCase, PaginationVO pageVo) {

		return userCaseDao.selectInPage(userCase, pageVo);

	}

	@Override
	public List<UserCase> findByModel(UserCase userCase) {

		return userCaseDao.selectByModel(userCase);

	}

	@Override
	public int findTotal(UserCase userCase) {

		return userCaseDao.selectTotalRecord(userCase);

	}

	@Override
	public UserCase findById(Long caseId) {

		return userCaseDao.selectById(caseId);

	}

	@Override
	public Integer findMaxNo(Long projId) {
		Integer i = userCaseDao.selectMaxNo(projId);
		return i == null ? 0 : i;
	}

	@Transactional
	@Override
	public void exeCase(UserCase userCase, Users user) {
		UserCase dataCase = userCaseDao.selectById(userCase.getCaseId());
		String caseStatus = "通过";
		boolean isSuccess = true;
		List<CaseStep> steps = new ArrayList<>();
		for (CaseStep caseStep : userCase.getStepsNoHistory()) {
			if (caseStep.getResult() != null) {
				caseStatus = caseStep.getResult();
				if (!caseStep.getResult().equals("通过")) {
					isSuccess = false;
					break;
				}
			}
		}

		if (isSuccess) {
			dataCase.setSuccess(dataCase.getSuccess() + 1);
		} else {
			dataCase.setFail(dataCase.getFail() + 1);
		}

		for (CaseStep caseStep : userCase.getStepsNoHistory()) {
			if (caseStep.getResult() != null) {
				caseStep.setHistoryNo(dataCase.getSuccess() + dataCase.getFail());
				caseStep.setExeUser(user.getName());
				caseStep.setCateId(userCase.getCaseId());
				caseStep.setExeTime(new Date());
				caseStep.setHistoryResult(caseStatus);
				steps.add(caseStep);
			}

		}
		// 插入历史记录
		if (!steps.isEmpty())
			stepDao.batchInsert(steps);
		dataCase.setCaseStatus(caseStatus);
		userCaseDao.updateByModel(dataCase);
	}

	@Override
	@Transactional
	public int putIn(UserCase userCase) {
		int i = userCaseDao.insert(userCase);		
		setStepsHas(userCase);
		return i;
	}
	
	/**
	 * 设置导入的通用用例的步骤
	 */
	private void setStepsHas(UserCase userCase) {
		// 删除historyNo为0的步骤
		CaseStep del = new CaseStep();
		del.setHistoryNo(0);
		del.setCateId(userCase.getCaseId());
		stepDao.deleteByModel(del);
		List<CaseStep> steps = new ArrayList<>();
		for (CaseStep caseStep : userCase.getStepsNoHistory()) {
			if (caseStep.getStepNo() != null) {
				caseStep.setCateId(userCase.getCaseId());
				caseStep.setHistoryNo(0);
				caseStep.setStepId(null);
				steps.add(caseStep);
			}
		}
		// 插入步骤
		if (!steps.isEmpty())
			stepDao.batchInsert(steps);
	}

	
	@Override
	@Transactional
	public int putInByExcel(UserCase userCase) {
		int i = userCaseDao.insert(userCase);		
		setStepsHasByExcel(userCase);
		return i;
	}
	
	/**
	 * 设置导入的通用用例的步骤
	 */
	private void setStepsHasByExcel(UserCase userCase) {
		// 删除historyNo为0的步骤
		CaseStep del = new CaseStep();
		del.setHistoryNo(0);
		del.setCateId(userCase.getCaseId());
		stepDao.deleteByModel(del);
		
		CaseStep caseStep = new CaseStep();
		caseStep.setCateId(userCase.getCaseId());
		caseStep.setTitle(userCase.getStepsNoHistory().get(0).getTitle());
		caseStep.setExpectation(userCase.getStepsNoHistory().get(0).getExpectation());
		caseStep.setHistoryNo(0);
		caseStep.setStepNo(1);
		caseStep.setStepId(null);
		
		stepDao.insert(caseStep);
	}

}
